package com.cyf.db.bean;

import com.cyf.smartCity.bean.FeeGroupTimeQuantum;
import com.cyf.smartCity.bean.FeeRule;
import com.cyf.smartCity.bean.TimezoneRule;
import com.cyf.util.ArithUtil;
import com.cyf.util.ObjectMapperUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.ArrayList;

/**
 * Created by hfy on 2017/7/24.
 */
@Data
public class LocalFeeGroup {

    public static final int BILLINGTYPE_MONTH = 1;
    public static final int BILLINGTYPE_STORED = 2;
    public static final int BILLINGTYPE_TEMP = 3;
    public static final int BILLINGTYPE_FREE = 4;
    public static final int BILLINGTYPE_COUPON = 5;
    public static final int BILLINGTYPE_BOOKING = 6;
    public static final int BILLINGTYPE_YEAR = 7;

    // 是否在前端软件显示，1显示 0不显示
    public static final int SHOW = 1;

    private int id;
    private String name; //feeType;
    private String feeDescrip; //feeDesc;
    private int isShow;
    private int localFeegroupId;
    private int billingType;
    private Integer fee;

    private Integer carValidTime;//车辆有效期 单位：月

    public boolean isMonthOrYear() {
        return this.billingType == BILLINGTYPE_MONTH || this.billingType == BILLINGTYPE_YEAR;
    }

    /**
     * 解析计费规则的json字符串
     *
     * @return
     */
    public String parseFeeJson() {
        // 1月租、2储值、3临时、4免费、5优惠券、6车位预约
        switch (this.billingType) {
            case BILLINGTYPE_FREE:
                return "免费";
            case BILLINGTYPE_MONTH:
                if (this.fee % 100 == 0) {
                    return "月卡" + (this.fee / 100) + "元";
                }
                return "月卡" + (this.fee * 0.01) + "元";
            case BILLINGTYPE_COUPON:
            case BILLINGTYPE_BOOKING:
                return "";
            default: {

            }
        }
        try {
            StringBuilder sb = new StringBuilder();
            FeeRule feeRule = this.getFeeRule();
            for (TimezoneRule timezoneRule : feeRule.getRules()) {
                if (timezoneRule.getType() == TimezoneRule.TYPE_TIME_SLOT) {
                    if (timezoneRule.getFee() % 100 == 0) {
                        sb.append(timezoneRule.getFee() / 100).append("元/");
                    } else {
                        sb.append(timezoneRule.getFee() * 0.01).append("元/");
                    }

                    if (feeRule.getRules().size() > 1) {
                        String start = division(timezoneRule.getTimeSlotBegin(), 3600);
                        String end = division(timezoneRule.getTimeSlotEnd(), 3600);
                        sb.append(start).append("-").append(end).append("小时");
                    }
                } else {
                    if (feeRule.getRules().size() > 1) {
                        sb.append(timezoneRule.getRealbegin()).append("-").append(timezoneRule.getRealend()).append(" ");
                    }
                    if (timezoneRule.getFee() % 100 == 0) {
                        sb.append(timezoneRule.getFee() / 100).append("元/");
                    } else {
                        sb.append(timezoneRule.getFee() * 0.01).append("元/");
                    }
                    // 按时计费
                    if (timezoneRule.getType() == TimezoneRule.TYPE_HOUR) {
                        // cfreeTimeType // 0是分钟 1是小时
                        if (timezoneRule.getCfreeTimeType() == TimezoneRule.CFREETIMETYPE_MINUTE) {
                            sb.append(timezoneRule.getUnit() / 60).append("分钟");
                        } else {
                            sb.append(timezoneRule.getUnit() / 3600).append("小时");
                        }
                    } else if (timezoneRule.getType() == TimezoneRule.TYPE_SECOND) {
                        sb.append("次");
                    }
                    if (timezoneRule.getbFreeComp() == TimezoneRule.BFREECOMP_NOT_CACULATE_FEE && timezoneRule.getFreeTime() > 0) {
                        sb.append(",免费");
                        if (timezoneRule.getFreeTimeType() == TimezoneRule.FREETIMETYPE_HOUR) {
                            sb.append(timezoneRule.getFreeTime() / 3600).append("小时");
                        } else {
                            sb.append(timezoneRule.getFreeTime() / 60).append("分钟");
                        }
                    }
                }
                sb.append(";");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private String division(int num, int num1) {
        String str;
        if (num % num1 == 0) {
            str = num / num1 + "";
        } else {
            str = ArithUtil.div(num, num1, 2) + "";
        }
        return str;
    }


    /**
     * 获取计费规则
     *
     * @return FeeRule
     * @throws Exception 异常
     */
    public FeeRule getFeeRule() throws Exception {
        // 非月卡，免费，有计费字符串
        if (null == this.feeDescrip) {
            throw new Exception("解析计费规则失败");
        }
        ObjectMapper reqMapper = ObjectMapperUtils.getObjectMapper();
        FeeRule feeRule = reqMapper.readValue(this.feeDescrip,
                FeeRule.class);
        if (null == feeRule) {
            throw new Exception("计费规则格式化失败");
        }
        // 解析 timeQuant      um
        if (feeRule.getParseType() != null && feeRule.getParseType() != FeeRule.PARSE_TYPE_RULES) {
            // 将计费规则设置到 FeeRule中，这样，计费代码无需大改动。
            ArrayList<TimezoneRule> rules = new ArrayList<>();
            for (FeeGroupTimeQuantum timeQuantum : feeRule.getTimeQuantum()) {
                TimezoneRule rule = new TimezoneRule();
                rule.setType(TimezoneRule.TYPE_TIME_SLOT);
                rule.setTimeSlotBegin((int) Double.parseDouble(timeQuantum.getTimeSlotBegin()));
                rule.setTimeSlotEnd((int) Double.parseDouble(timeQuantum.getTimeSlotEnd()));
                if (timeQuantum.getTimeIncomplete() != null) {
                    rule.setIncomplete(timeQuantum.getTimeIncomplete());
                }
                rule.setFee(timeQuantum.getTimeSlotFee().intValue());
                rules.add(rule);
            }
            feeRule.setRules(rules);
        }
        return feeRule;
    }


    public String getBillingTypeDesc() {
        switch (this.billingType) {
            // 1月租、2储值、3临时、4免费、5优惠券、6车位预约、7年卡
            case BILLINGTYPE_MONTH:
                return "月卡车";
            case BILLINGTYPE_STORED:
                return "登记车";
            case BILLINGTYPE_TEMP:
                return "临时车";
            case BILLINGTYPE_FREE:
                return "免费车";
            case BILLINGTYPE_YEAR:
                return "年卡车";
            default: {
                return "临时车";
            }
        }
    }
}
